<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['web:log:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['web:log:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['web:log:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['web:log:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="10">
      <!-- <el-col :span="6">
        <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="标题" align="center" prop="remark" width="180" />
          <el-table-column label="时间" align="center" prop="createTime" width="160" />
          <el-table-column label="日志内容" align="center" prop="logData"></el-table-column>
          <el-table-column prop="logData" label="内容">
            <template #default="{ row }">
              <TiptapRenderer :content="row.logData" />
            </template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['web:log:edit']">修改</el-button>
              <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['web:log:remove']">删除</el-button>
            </template>
</el-table-column>
</el-table>
<pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
  @pagination="getList" />
</el-col> -->
      <el-col :span="8" style="padding: 10px;">
        <div v-for="item in logList" :key="item.id">
          <el-card style="max-width: 480px ;padding: auto;margin: 10px;" shadow="always">
            <el-descriptions :column="12">
              <el-descriptions-item :span="6">{{ item.remark }} </el-descriptions-item>
              <el-descriptions-item :span="3"> {{ item.createTime }} </el-descriptions-item>
              <el-descriptions-item :span="3">
                <el-button link type="primary" icon="View" @click="handleView(item)">查看</el-button>
              </el-descriptions-item>
            </el-descriptions>
          </el-card>
        </div>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-col>
      <el-col :span="16">
        <el-row>
          <el-col :span="12">
            <div style="font-size: 25px; color: #409EFF; margin-top: 30px;">{{ logTitle }}</div>
          </el-col>
          <el-col :span="6">
            <div style="font-size: small; color: #606266;margin-top: 30px;">{{ logDate }}</div>
          </el-col>
        </el-row>
        <div v-html="stuHobbyContent"></div>
      </el-col>
    </el-row>




    <!-- 添加或修改股票日志对话框 -->
    <el-dialog :title="title" v-model="open" width="1500px" append-to-body>
      <el-form ref="logRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item label="标题" prop="remark">
              <el-input v-model="form.remark" placeholder="标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-form-item label="日志内容1">
            <editor v-model="form.logData" :min-height="892" :width="1000" />
          </el-form-item>

        </el-row>
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="明日买" prop="yuBuy">
              <el-autocomplete v-model="form.yuBuy" :fetch-suggestions="querySearchGuPiaoList" placeholder="请输入 guName"
                @select="handleSelect" clearable value-key="guName" @clear="handleClear"></el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="预计价格" prop="yuBuyPrice">
              <el-input v-model="form.yuBuyPrice" placeholder="请输入" >
                <template #append>元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="数量" prop="yuNum">
              <el-input v-model="form.yuNum" placeholder="数量" >
                <template #append>手</template>
              </el-input>
            </el-form-item>
          </el-col>
      
          <el-col :span="4">
            <el-form-item label="明日卖" prop="yuCell">
              <el-autocomplete v-model="form.yuCell" :fetch-suggestions="querySearchGuPiaoList" placeholder="请输入 guName"
                @select="handleSelect" clearable value-key="guName" @clear="handleClear"></el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="预计价格" prop="yuCellPrice">
              <el-input v-model="form.yuCellPrice" placeholder="请输入" >
                <template #append>元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="数量" prop="yuCellNum">
              <el-input v-model="form.yuCellNum" placeholder="数量" >
                <template #append>手</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="查阅" v-model="openView" width="60%" append-to-body :close-on-click-modal="false">
      <el-card class="w-e-text">
        <div v-html="stuHobbyContent"></div>
      </el-card>
      <div slot="footer" class="dialog-footer" style="margin: 10px;">
        <el-button type="primary" @click="openView = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Log">
import { listLog, getLog, delLog, addLog, updateLog } from "@/api/web/log"
import TiptapRenderer from '@/components/Editor/TiptapRenderer.vue'
import { column } from "element-plus/es/components/table-v2/src/common.mjs"
import { ref } from "vue"
import { listGupiao, listGupiaoAll } from "@/api/web/gupiao"
const { proxy } = getCurrentInstance()

const logList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const openView = ref(false)
const stuHobbyContent = ref("")
const logTitle = ref("")
const logDate = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    logData: null,
  },
  rules: {
  },
})
// 从API获取股票数据
const querySearchGuPiaoList = async (queryString, cb) => {

  try {
    const response = await listGupiaoAll();
    cb(response.rows);
  } catch (error) {
    console.error('搜索失败:', error);
    cb([]);
  }
};
const { queryParams, form, rules } = toRefs(data)

/** 查询股票日志列表 */
function getList() {
  loading.value = true
  listLog(queryParams.value).then(response => {
    logList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    pageNum: 1,
    pageSize: 10,
    logData: null,
    yuBuy: null,
    yuNum: null,
    yuCell: null,
    bankuai: null,
    yuBuyPrice: null,
    yuCellNum: null,
    yuCellPrice: null
  }
  proxy.resetForm("logRef")
}

function handleView(item) {
  //openView.value = true;
  stuHobbyContent.value = item.logData;
  logTitle.value = item.remark;
  logDate.value = item.createTime;

}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加股票日志"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getLog(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改股票日志"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["logRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateLog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除股票日志编号为"' + _ids + '"的数据项？').then(function () {
    return delLog(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('web/log/export', {
    ...queryParams.value
  }, `log_${new Date().getTime()}.xlsx`)
}

getList()
</script>
